package com.c.yinyuezhushou.Adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.arialyy.aria.core.download.DownloadEntity;
import com.bumptech.glide.Glide;
import com.c.yinyuezhushou.R;
import com.c.yinyuezhushou.Service.DownService;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class DownRecAdapter2 extends RecyclerView.Adapter {

    private List<DownloadEntity> list;
    private DownService downService;

    public DownRecAdapter2(List<DownloadEntity> list, DownService downService) {
        this.list = list;
        this.downService =downService;
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        TextView textView1;
        TextView textView2;
        ImageView imageView;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            textView1 = itemView.findViewById(R.id.idr2_text1);
            textView2 = itemView.findViewById(R.id.idr2_text2);
            imageView = itemView.findViewById(R.id.idr2_image);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_down_recycler2,parent,false);
        DownRecAdapter2.ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        DownRecAdapter2.ViewHolder viewHolder = (DownRecAdapter2.ViewHolder) holder;
        JSONObject jsonObject = null;
        try {
            if (list.get(position).getStr()!=null){
                jsonObject = new JSONObject(list.get(position).getStr());
                viewHolder.textView1.setText(jsonObject.getString("song"));
                viewHolder.textView2.setText(jsonObject.getString("singer"));
                Log.d("test1",jsonObject.getString("imageurl"));
                Glide.with(viewHolder.imageView.getContext()).load(StringUtils.remove(jsonObject.getString("imageurl"), "%2B")).into(viewHolder.imageView);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    @Override
    public int getItemCount() {
        if (list!=null){
            return list.size();
        }
        return 0;
    }

    public void setList(List<DownloadEntity> list){
        this.list = list;
    }
}
